#ifndef GAME_H
#define GAME_H



#include "init.h"
#include "players.h"
#include "units.h"
#include <vector>
#include <QObject>



class object;

class player;

class game_state;



class mapa
{

public:

	static mapa* get_instance();
	int x;

	void set_object( object* o, unsigned int x, unsigned int y );
	object* get_object( unsigned int x, unsigned int y );

	bool load( const char* file );
	bool load_objects( const char* file );

	void save_objects( const char* file );

	array2d< unsigned int >* terrain_clone();

	static void export_map( array2d< unsigned int >* map, const char* file, unsigned int sx, unsigned int sy );

	unsigned int get_size_x() { return size_x; };
	unsigned int get_size_y() { return size_y; };
	
	array2d< object* >* objects;
	array2d< unsigned int >* terrain;


private:

	mapa( int _x ):x( _x ){};
	mapa( const mapa& );
	mapa& operator=( const mapa& x );

	bool parse_objects_line( std::string ol );

	unsigned int size_x;
	unsigned int size_y;

};



class players
{

public:

	static players* get_instance();
	void fill_players();

	void set_player( player* p, unsigned int x );
	player* get_player( unsigned int x );
	unsigned int get_players_count() { return players_count; };
	void set_players_count( unsigned int c ) { players_count = c; };
	
	player** pl;

	int x;


private:

	players( int _x ): x( _x )
	{
		pl = new player*[ parameters::get_instance()->game_parameters[ "MAX_PL" ] ];
		players_count = 0;
	};
	players( const players& );
	players& operator=( const players& x );

	unsigned int players_count;
	
};



class game: public QObject
{

	Q_OBJECT

public:

	static game* get_instance();

	static std::string create_command( std::string c, unsigned int u, unsigned int x, unsigned int y );

	void get_next_ai_command();
	void get_next_ai_command_now();

	void start_game();
	void next_turn();
	player* test_game_end();

	player* get_current_player();
	void set_current_player( unsigned int n );
	
	bool can_play( const player* p );

	bool is_paused() { return ai_paused; };
	void set_paused( bool paused );


	game_state* get_current_state();


	void set_score_to_history( game_state* state );
	std::vector< std::vector< double > >* get_score_history();


	int x;


private:

	game( int _x ): x( _x )
	{
		current_player = players::get_instance()->get_player( 0 );
		ai_paused = true;
	};
	game( const game& );
	game& operator=( const game& x );

	player* current_player;

	bool ai_paused;

	game_state* current_state;
	bool up_to_date;

	std::vector< std::vector< double > > score_history;


public slots:

	void command_received( player* pl, std::string command );

};



#endif